<?php


class handleCoupon extends IController
{
    private $limit = 500;
    private $logObj;


    public function init()
    {
        $this->logObj = new Log('file');
    }

    /**
     * 开始处理创建优惠券
     */
    public function create()
    {
        $queue_name     = "handel_coupon_queue";
        $redis_obj      = new redisMq();
        $message_length = $redis_obj->getMessageLength($queue_name);
        if ($redis_obj->hasError()) {
            $this->logObj->cliWrite('error', array("读取redis队列失败：" . var_export($redis_obj->getError(), true)));
            exit;
        }
        if (empty($message_length)) {
            exit;
        }
        //可以配置每次消费几条
        for ($i = 0; $i < $this->limit; $i++) {
            $result = $redis_obj->pop($queue_name);
            //如果发生异常
            if ($redis_obj->hasError()) {
                $this->logObj->cliWrite('error', array("读取redis队列失败：" . var_export($redis_obj->getError(), true)));
                exit;
            }
            //有内容的时候则进行处理
            if ($result['msg_id']) {
                $data = json_decode($result['msg_body'], true);
                //进行功能逻辑处理
                if ($data) {
                    $couponData = array(
                        'coupon_group_id' => $data['coupon_group_id'],
                        'code'            => $data['code'],
                        'send_type'       => $data['send_type'],
                        'create_time'     => $data['create_time'],
                    );
                    $couponDb   = new IModel('coupon');
                    $couponDb->setData($couponData);
                    $couponId = $couponDb->add();
                    if ($couponId === false) {
                        $this->logObj->cliWrite('error', array("优惠券生成失败！" . var_export($couponData, true)));
                    }
                }
                $redis_obj->remove($queue_name, $result['msg_id']);;
            } else {
                break;
            }
        }

    }

    /**
     * 检测优惠券过期
     */
    public function check()
    {
        $couponGroupsDb   = new IModel('coupon_groups');
        $nowDate          = ITime::getTime();
        $where            = "status = 1";
        $couponGroupsInfo = $couponGroupsDb->query($where, 'id,end_time,prop_sale,props_id,goods_sale,seller_id');
        if ($couponGroupsInfo) {
            $couponDb = new IModel('coupon');
            foreach ($couponGroupsInfo as $c_val) {
                if (strtotime($c_val['end_time']) < $nowDate) {
                    $couponWhere = "coupon_group_id ={$c_val['id']} and status in(1,2)";
                    $couponDb->setData(array('status' => 4));
                    $couponDb->update($couponWhere);
                    if ($c_val['goods_sale'] == 1) {
                        $goodsDb = new IModel('goods');
                        $goodsDb->setData(array(
                            'is_del'    => 2,
                            'down_time' => ITime::getDateTime()
                        ));
                        $goodsDb->update("coupon_group_id=" . $c_val['id']);
                    }
                    if ($c_val['prop_sale'] == 1) {
                        $zxlObj        = new  zxlhelper();
                        $propData      = array(
                            'coupon_props_id' => $c_val['props_id'],
                            'seller_id'       => $c_val['seller_id'],
                        );
                        $addPropResult = $zxlObj->couponDoFinish($propData);
                        if ($addPropResult === false) {
                            $this->logObj->cliWrite('error', array("优惠过期,强制结束失败！同步关闭道具失败"));
                        }
                    }
                    $couponGroupsDb->setData(array(
                        'status'      => 2,
                        'update_time' => ITime::getDateTime()
                    ));
                    $couponGroupsDb->update("id=" . $c_val['id']);
                }
            }
        }
    }


}